# -*- coding: utf-8 -*-

def calculate_elo(p1, p2, winner, bonus=False):
    """
    Racuna ELO prema http://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details
    """

    # konstanta
    K = 32
    # actual score
    if winner == 1:
        s1, s2 = 1, 0
    elif winner == 2:
        s1, s2 = 0.5, 0.5
    elif winner == 3:
        s1, s2 = 0, 1
    else:
        s1, s2 = 0, 0
    # calculate probabilities
    e1, e2 = calculate_probabilities(p1, p2)
    # new values
    offset1 = K*(s1-e1)
    offset2 = K*(s2-e2)
    if bonus == True:
        if offset1 > 0:
            offset1 = 1.5 * offset1
        else:
            offset1 = 0.5 * offset1
        if offset2 > 0:
            offset2 = 1.5 * offset2
        else:
            offset2 = 0.5 * offset2

    return (offset1, offset2)

def calculate_probabilities(p1, p2):
    """
    Vraca tuple float point verovatnoca da ce dobiti prvi ili drugi igrac
    """
    # q values
    q1 = 10**(p1/400.0)
    q2 = 10**(p2/400.0)
    # expected values
    e1 = q1/(q1+q2)
    e2 = q2/(q1+q2)

    return (e1,e2)